function longer(u,v){v=v.toString();while(v.length<u)v='0'+v;return v;}
function gethtml(title,diff,rank,num,mxp,mis,far,rankk)
{
	var str='<div class="ui cards" style="display:inline-block;margin-top:-1.5em;margin-bottom:0.4em;margin-right:0.33em;"><div class="record card"><div class="content"><div class="header"><div class="title">';
	str+=title;
	str+='</div><span class="ui '+['blue','green','purple','red'][diff]+' horizontal label">';
	str+=['PST','PRS','FTR','BYD'][diff]+' ';
	var s=Math.floor(rank+0.05).toString();
	if(Math.floor(rank+0.05)>=9) if(rank-Math.floor(rank+0.05)>=0.65) s+='+';
	if(title=='dropdead'&&diff==2) s='8';
	if(title=='Moonheart'&&diff==3) s='9+';
	if(title=='Final Step!'&&diff==1) s='6';
	if(title=='World Vanquisher'&&diff==2) s=['1','6','8','10+'][Math.random()*4|0];
	str+=s;
	str+='</span></div><div class="description"><div class="ui very compact grid"><div class="row"><div class="score"><span class="score">';
	var pur=num-mis-far;
	var score=Math.floor(1e7*(pur+far/2)/num+mxp);
	var a=Math.floor(score/1e6),b=Math.floor(score/1e3)%1000,c=score%1000;
	str+=longer(2,a)+"'"+longer(3,b)+"'"+longer(3,c);
	var ptt=Math.max(0,rank+Math.min((score>=9800000?(score-9600000)/200000:(score-9500000)/300000),2));
	ptt=Math.floor(ptt*100000)/100000;
	str+='</span><span class="chart-ptt"> / '+rank+' / '+ptt;
	str+='</span></div></div><div class="row"><div class="three wide rank column">';
	var rk='D';
	if(score>=8600000) rk='C';
	if(score>=8900000) rk='B';
	if(score>=9200000) rk='A';
	if(score>=9500000) rk='AA';
	if(score>=9800000) rk='EX';
	if(score>=9900000) rk='EX+';
	if(score>=10000000) rk='PM';
	if(score>=10000000+num) rk='PM+';
	str+=rk;
	str+='</div><div class="nine wide details column"><div class="ui very compact grid"><div class="row">';
	str+='P: '+pur+' (+'+mxp+')'+'</div><div class="equal width row"><div class="column">';
	str+='F: '+far+'</div><div class="column">';
	str+='L: '+mis+'</div></div></div></div>';
	str+='<div class="three wide rank column">'+rankk+'</div>';
	str+='</div></div></div></div></div></div>';
	return str;
}
var listsong;
function calcptt(str)
{
	var t=JSON.parse(Base64.decode(str));
	var playlist0=t.playlist;
	var songlist0=t.songlist;
	var ptt=0;
	for(var i in playlist0)
	{
		var pttnow=getptt(songlist0[playlist0[i].title][playlist0[i].diff].rank,songlist0[playlist0[i].title][playlist0[i].diff].num
			,playlist0[i].mxp,playlist0[i].mis,playlist0[i].far);
		if(i<10) ptt+=pttnow;
		if(i<30) ptt+=pttnow;
	}
	ptt/=40;
	return ptt;
}
function show()
{
	if(Object.keys(recentplay).length!=0) document.getElementById("recent").innerHTML=gethtml(recentplay.title,recentplay.diff,songlist[recentplay.title][recentplay.diff].rank,
		songlist[recentplay.title][recentplay.diff].num,recentplay.mxp,recentplay.mis,recentplay.far,'REC');
	else document.getElementById("recent").innerHTML="暂无最近游玩记录";
	var ptt=0;document.getElementById("all").innerHTML='';
	for(var i in playlist)
	{
		document.getElementById("all").innerHTML+=gethtml(playlist[i].title,playlist[i].diff,songlist[playlist[i].title][playlist[i].diff].rank,
			songlist[playlist[i].title][playlist[i].diff].num,playlist[i].mxp,playlist[i].mis,playlist[i].far,'#'+(parseInt(i)+1));
		var pttnow=getptt(songlist[playlist[i].title][playlist[i].diff].rank,songlist[playlist[i].title][playlist[i].diff].num
			,playlist[i].mxp,playlist[i].mis,playlist[i].far);
		if(i<10) ptt+=pttnow;
		if(i<30) ptt+=pttnow;
	}
	ptt/=40;
	var hi=Math.floor(ptt),lo=Math.floor(100*(ptt-hi));
	lo=lo.toString();hi=hi.toString();
	var badge=0;
	if(ptt>=3.5) badge=1;
	if(ptt>=7) badge=2;
	if(ptt>=10) badge=3;
	if(ptt>=11) badge=4;
	if(ptt>=12) badge=5;
	if(ptt>=12.5) badge=6;
	document.getElementById("ptt").className="rating-small rating-badge-"+badge;
	document.getElementById("ptthi").innerHTML=hi+'.';document.getElementById("pttlo").innerHTML=longer(2,lo);
	document.getElementById('pttext').innerHTML=hi+'.'+longer(5,Math.floor((ptt-hi)*1e5).toString());
	var songcur='';
	listsong=[];
	for(i in songlist) listsong.push({'title':i,'info':songlist[i]});
	listsong.sort(function(a,b){return (a.title>b.title)?1:-1;});
	for(i in listsong)
	{
		songcur+='<div class="title"><div class="ui grid"><div class="six wide column">'+listsong[i].title+'</div>';
		songcur+='<div class="nine wide column"><div class="ui equal width grid">';
		for(var j=0;j<4;++j)
		{
			if(listsong[i].info[j])
				songcur+='<div class="column"><div class="ui '+['blue','green','purple','red'][j]+' horizontal mini label">'+
			Math.floor(listsong[i].info[j].rank)+'.'+(listsong[i].info[j].rank*10)%10+'</div>'+listsong[i].info[j].num+'</div>';
			else songcur+='<div class="column"></div>';
		}
		songcur+="<div class='column'><div class='mini ui blue submit button' onclick='jumpToPlayUpload("+i+")'>录入成绩</div></div>";
		songcur+='</div></div></div></div>';
	}
	document.getElementById('songcur').innerHTML=songcur;
	/*
	<p style="width:28em;float:left;vertical-align: middle;">WYXkk-thumb</p>
    <div style="display:inline;margin-left:0em;"><div class="rating-small rating-badge-3" id="ptt" style="vertical-align: middle;">
	<span><span class="hi" id="ptthi">10.</span><span class="lo" id="pttlo">78</span></span></div>
    <p style="font-style: italic;font-size: 0.75em;" id="pttext">10.78304</p><br>
    */
    var savescur="";
    for(i in othersaves)
    {
    	ptt=calcptt(othersaves[i]);
    	savescur+='<p style="width:28em;float:left;vertical-align: middle;">'+i+'</p>';
    	var badge0=0;if(ptt>=3.5) badge0=1;if(ptt>=7) badge0=2;if(ptt>=10) badge0=3;
    	if(ptt>=11) badge0=4;if(ptt>=12) badge0=5;if(ptt>=12.5) badge0=6;
    	savescur+='<div style="display:inline;margin-left:0em;"><div class="rating-small rating-badge-'+badge0+'" style="vertical-align: middle;">';
    	hi=Math.floor(ptt),lo=Math.floor(100*(ptt-hi));lo=lo.toString();hi=hi.toString();
    	savescur+='<span><span class="hi">'+hi+'.</span><span class="lo">'+longer(2,lo)+'</span></span></div>';
    	savescur+='<p style="font-style: italic;font-size: 0.75em;">'+hi+'.'+longer(5,Math.floor((ptt-hi)*1e5).toString())+'</p><br>';
    }
    document.getElementById('othersavelist').innerHTML=savescur
}
function jumpToPlayUpload(i)
{
	settab(2,'playupload');
	scrollTo(0,0);
	document.getElementById('songtitle').value=listsong[i].title;
	document.getElementById("searcha").href=searchlink+listsong[i].title;
}